﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Media.Imaging;
using System.Diagnostics;
using PhoneInfo.Converters;


namespace PhoneInfo.Page
{
    public partial class SoftDetail_Screen : PhoneApplicationPage
    {
        ProgressIndicator _progress = new ProgressIndicator() { IsVisible = false, IsIndeterminate = true };

        double initialAngle;
        double initialScale;
        int index = 0;
        Arg arg = new Arg();

        public SoftDetail_Screen()
        {
            InitializeComponent();
            this.SetValue(SystemTray.OpacityProperty, 0.00001);
            Loaded += new RoutedEventHandler(SoftDetail_Screen_Loaded);
        }

        void SoftDetail_Screen_Loaded(object sender, RoutedEventArgs e)
        {
            SystemTray.ProgressIndicator = _progress;
            _progress.IsVisible = true;
            if (App.obj == null) { MessageBox.Show("暂无图片，请返回！"); NavigationService.GoBack(); return; }
            arg = (Arg)App.obj;
            img.Source = arg.list[arg.selectIndex];
            index = arg.selectIndex;
            _progress.IsVisible = false;
        }

        private void OnPinchStarted(object sender, PinchStartedGestureEventArgs e)
        {
            //initialAngle = transform.Rotation;
            //initialScale = transform.ScaleX;
        }

        private void OnPinchDelta(object sender, PinchGestureEventArgs e)
        {
            //double a = initialScale * e.DistanceRatio;
            //double d = a > 1.8 ? 1.8 : a;
            //d = d < 0.5 ? 0.5 : d;
            //transform.ScaleX = transform.ScaleY = d;
        }

        private void GestureListener_DoubleTap(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
        {
            //transform.TranslateX = transform.TranslateY = 0;
            //transform.ScaleX = transform.ScaleY = 1;
        }

        private void OnHold(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
        {
            //transform.TranslateX = transform.TranslateY = 0;
            //transform.ScaleX = transform.ScaleY = 0;
            //transform.Rotation = 0;
        }
        private void OnDragDelta(object sender, DragDeltaGestureEventArgs e)
        {
            transform.TranslateX += e.HorizontalChange;
        }

        private void GestureListener_DragCompleted(object sender, DragCompletedGestureEventArgs e)
        {
            DoubleAnimation d = (DoubleAnimation)sto_left.Children[0];
            double width = ((BitmapImage)img.Source).PixelWidth;
            DoubleAnimation d2 = (DoubleAnimation)sto_right.Children[0];
            double width2 = ((BitmapImage)img.Source).PixelWidth;
            if (arg.list.Count < index + 2)
                index = 0;
            else
                index++;

            if (transform.TranslateX < -90)  //向左翻
            {
                d.To = -(width + (480 - width) / 2);
                sto_left.Begin();
                sto_left.Completed += (s, ee) =>
                {
                    img.Source = arg.list[index];
                    d2.To = 0;
                    d2.From = width2 + (480 - width2) / 2;
                    sto_right.Completed += (_, __) => { transform.TranslateX = 0; };
                    sto_right.Begin();
                };
            }
            else if (transform.TranslateX > 90) //向右翻
            {
                d.To = width + (480 - width) / 2;
                sto_left.Begin();
                sto_left.Completed += (s, ee) =>
                {
                    img.Source = arg.list[index];
                    d2.To = 0;
                    d2.From = -(width2 + (480 - width2) / 2);
                    sto_right.Begin();
                    sto_right.Completed += (_, __) => { transform.TranslateX = 0; };
                };
            }
        }

    }
}